<?php
include 'includes/cDatabase.php';
include 'includes/cPlayer.php';
include 'includes/variables.php';
include 'includes/cPage.php';
include 'includes/cDates.php';

define("TD_NAME", "<td width='800' align='center'>");
define("TD_MAINNAME", "<td width='200' align='center'>");
define("TD_PLAYER", "<td width='200' align='center'>");
define("TD_NORMAL", "<td width='100' align='center'>");
define("TD_JPEG", "<td width='100' align='center'>");
define("TD_SCORE", "<td width='50' align='center'>");

$cPage = new page("Players' Scores");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

if (isset($_GET["localPlayerCode"]))
{
	$playerCode = @$_GET['localPlayerCode'];
	$setValue = $_POST['code'];
	
	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);
	
	$maxWeeks = $cDatabase->GetMaxWeeks($connect);
	
	$cPlayer = new player($playerCode);
	$cPlayer->setPlayerTeam($connect);
	$cPlayer->setPlayerActive($connect);
	$path = $cPlayer->shirtLookup();
	
	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, "playerCode", $playerCode, NULL, NULL);
	
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	
	$playerScore = getPlayerScore($row, $maxWeeks);

	if (!$cPlayer->getPlayerActive())
	{
		$playerActive = 'NO';
		$colour = GREY;
	}		
	else
	{
		$playerActive = 'YES';
		$colour = BLUE;
	}
			
	// START Main table
	echo "<div align='center'>";
	echo "<table width='1000' border='1'>";
	
	// START This is the first two rows ***********************************************************
		echo "<tr>";
			// Player Shirt
			echo "<td rowspan='2' colspan='1'><table>";
				echo "<tr>";	
					echo TD_JPEG."<img src='".$path."' width='50' height='50'></td>";
				echo "</tr>";
			echo "</table></td>";
		
			// Player Name and Score
			echo "<td rowspan='1' colspan='4'><table>";
				echo "<tr>";
					echo TD_NAME.$colour.$row['playerName']."</font></td>";	
					echo TD_NAME."Total Points : ".$colour.$playerScore."</font></td>";
					echo TD_NAME."Active : ".$colour.$playerActive."</font></td>";
					echo TD_NAME."</td>";
					
				echo "</tr>";
			echo "</table></td>";
		echo "</tr>";
		
		// Player Details
		echo "<td rowspan='1' colspan='4'><table>";
		
			echo "<tr>";
				echo TD_PLAYER."Code : ".$colour.$playerCode."<font></td>";
				echo TD_PLAYER."Position : ".$colour.$row['playerPosition']."<font></td>";
				echo TD_PLAYER."Team : ".$colour.$row['playerTeam']."<font></td>";
				echo TD_PLAYER."Value : ".$colour.$row['playerValue']."m<font></td>";	
			echo "</tr>";
			
			$cPlayer->loadPlayerOccurrence($connect);
			
			echo "<tr>";
				echo TD_NAME."Currently selected by : ".$colour.$cPlayer->Current." person(s)</font></td>";
				echo TD_NAME."Transferred out by : ".$colour.$cPlayer->Out." person(s)</font></td>";
				echo TD_NAME."Transferred in by : ".$colour.$cPlayer->In." person(s)</font></td>";
				echo TD_NAME."</td>";
			echo "</tr>";
		echo "</table></td>";
	// END first two rows ***************************************************************************

	// START row 3 ********************************************************************************** Score Details weeks 1-20 ***
		echo "<tr>";
			echo "<td rowspan='1' colspan='5'><table>";
				echo "<tr>";
					echo "<td>WEEK</td>";
					for ($j = 1; $j <= ($maxWeeks/2); $j++)
						echo TD_SCORE.$j."</td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td>SCORE</td>";
					for ($j = 1; $j <= ($maxWeeks/2); $j++)
					{
						if ($j < 10)
							$week = "week0".$j;
						else
							$week = "week".$j;
						if ($row[$week] == NULL)
							$score = '-';
						else
							$score = $row[$week];
						echo TD_SCORE.BLUE.$score."</font></td>";
					}
				echo "</tr>";
			echo "</table></td>";
		echo "</tr>";
	// END row 3 ************************************************************************************ Score Details weeks 1-20 ***
	
	// START row 4 ********************************************************************************** Score Details weeks 21-40 ***
		echo "<tr>";
			echo "<td rowspan='1' colspan='5'><table>";
				echo "<tr>";
					echo "<td>WEEK</td>";
					for ($j = ($maxWeeks/2)+1; $j <= $maxWeeks; $j++)
						echo TD_SCORE.$j."</td>";
				echo "</tr>";
				echo "<tr>";
					echo "<td>SCORE</td>";
					for ($j = ($maxWeeks/2)+1; $j <= $maxWeeks; $j++)
					{
						if ($j < 10)
							$week = "week0".$j;
						else
							$week = "week".$j;
						if ($row[$week] == NULL)
							$score = '-';
						else
							$score = $row[$week];
						echo TD_SCORE.BLUE.$score."</font></td>";
					}	
				echo "</tr>";
			echo "</table></td>";
		echo "</tr>";
	// END row 4 ************************************************************************************ Score Details weeks 21-40 ***
	
	// START row 5 ********************************************************************************** Graphs ***
		echo "<tr>";
			echo "<td rowspan='1' colspan='5'><table>";
				echo "<tr>";
					echo "<td><img src='JPGraph\DB_PlayerAccumPointsGraph.php?playerCode=".$playerCode."' border=0 align=center width=500 height=300></td>";
					echo "<td><img src='JPGraph\DB_PlayerPointsGraph.php?playerCode=".$playerCode."' border=0 align=center width=500 height=300></td>";
				echo "</tr>";
			echo "</table></td>";
		echo "</tr>";
	// END row 5 ************************************************************************************ Graphs ***

	echo "</table>";
	echo "</div>";
	// END Main table
}
else
{
	$PHP_SELF = $_SERVER['PHP_SELF'];
	
	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);
	$selectArray[0] = '*';
	
	$maxWeeks = $cDatabase->GetMaxWeeks($connect);
	
	if ((isset($_GET["plus"])) && ($_GET["plus"] != 'playerScore'))
	{
		$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, SORT_ASCENDING, @$_GET['plus']);
	}
	else if ((isset($_GET["minus"]))&& ($_GET["minus"] != 'playerScore'))
	{
		$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, SORT_DESCENDING, @$_GET['minus']);
	}
	else
	{
		$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL);
	}
	if($result)
	{
		echo "<div align='center'>";
		echo "<table border='1'><thead><tr>";
		echo "<th></th>";
		
		$finfo = mysqli_fetch_fields($result);
		for($i = 0; $i < mysqli_num_fields($result); $i++)
		{
			if($finfo[$i]->name == 'playerCode' || $finfo[$i]->name == 'playerName' || $finfo[$i]->name == 'playerTeam' || $finfo[$i]->name == 'playerValue' || $finfo[$i]->name == 'playerPosition')
			{
				switch ($finfo[$i]->name)
				{
				case 'playerCode'		: 	$heading = 'Code';
											break;
				case 'playerName'		: 	$heading = 'Name';
											break;
				case 'playerTeam'		: 	$heading = 'Team';
											break;
				case 'playerValue'		: 	$heading = 'Value';
											break;
				case 'playerPosition'	: 	$heading = 'Position';
											break;
				default					:	$heading = '';
											break;
				}
				$temp = $finfo[$i]->name;
				echo "<th>".$heading."";
				echo "<FORM ACTION='$PHP_SELF?plus=$temp' METHOD='POST'>";
				echo "<input name='headingname' type='submit' id='temp' value='+'>";
				echo "</form>";
				echo "<FORM ACTION='$PHP_SELF?minus=$temp' METHOD='POST'>";
				echo "<input name='headingname' type='submit' id='temp' value='-'>";
				echo "</form>";
				echo "</th>";
			}
		}
		$temp = 'playerScore';
		echo "<th>Transferred In</th>";
		echo "<th>Transferred Out</th>";
		echo "<th>Popularity</th>";
		echo "<th>Score";
		//echo "<FORM ACTION='$PHP_SELF?plus=$temp' METHOD='POST'>";
		//echo "<input name='headingname' type='submit' id='temp' value='+'>";
		//echo "</form>";
		//echo "<FORM ACTION='$PHP_SELF?minus=$temp' METHOD='POST'>";
		//echo "<input name='headingname' type='submit' id='temp' value='-'>";
		//echo "</form>";
		echo "</th>";
		echo "</tr></thead><tbody></form>";

		for ($i = 0; $i < mysqli_num_rows($result); $i++)
		{
			$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
			$playerCode = $row['playerCode'];
			$playerScore = getPlayerScore($row, $maxWeeks);
			if ($row['playerActive'] == 0)
			{
				$colour = GREY;
			}		
			else
			{
				$colour = BLUE;
			}
			
			$cPlayer[$i] = new player($row['playerCode']);
		
			$error |= $cPlayer[$i]->addPlayerName($row['playerName']);
			$error |= $cPlayer[$i]->addPlayerTeam($row['playerTeam']);
			$error |= $cPlayer[$i]->addPlayerValue($row['playerValue']);
			$error |= $cPlayer[$i]->addPlayerPosition($row['playerPosition']);
			$error |= $cPlayer[$i]->addPlayerLastYearScore($row['playerLastYearScore']);
			$cPlayer[$i]->loadPlayerOccurrence($connect);
			
			// Find out if this player is injured
			$selectArray[0] = '*';
			//echo "<BR>check ".$row['playerName'];
			$injuryResult = $cDatabase->selectFromDatabase($connect, 'injury', $selectArray, "player", $row['playerName'], NULL, NULL);
			$injuryRow = mysqli_fetch_array($injuryResult, MYSQLI_ASSOC);
			if (@mysqli_num_rows($injuryResult) != 0)
			{
				$injured = 1;
				$colour = RED;
			}
			else
			{
				$injured = 0;
			}
			
			echo "<FORM ACTION=\"$PHP_SELF?localPlayerCode=$playerCode\" METHOD=\"POST\">";
			
			if ($injured == 1)
			{
				$injuryInfo = $injuryRow['injury']." expected back on ".$injuryRow['returnDate'];
				
				echo "<tr>".TD_NORMAL."<input name='code' type='submit' id='playerCode' value='INJURED'></td>";
				echo TD_NORMAL.$colour."<span title=\"".$injuryInfo."\">".$row['playerCode']."</span></font></td>";
				echo TD_MAINNAME.$colour."<span title=\"".$injuryInfo."\">".$row['playerName']."</span></font></td>";
				echo TD_NORMAL.$colour."<span title=\"".$injuryInfo."\">".$row['playerTeam']."</span></font></td>";
				echo TD_NORMAL.$colour."<span title=\"".$injuryInfo."\">".$row['playerValue']."</span></font></td>";
				echo TD_NORMAL.$colour."<span title=\"".$injuryInfo."\">".$row['playerPosition']."</span></font></td>";
			}
			else
			{
				echo "<tr>".TD_NORMAL."<input name='code' type='submit' id='playerCode' value='Show Details'></td>";
				echo TD_NORMAL.$colour.$row['playerCode']."</font></td>";
				echo TD_MAINNAME.$colour.$row['playerName']."</font></td>";
				echo TD_NORMAL.$colour.$row['playerTeam']."</font></td>";
				echo TD_NORMAL.$colour.$row['playerValue']."</font></td>";
				echo TD_NORMAL.$colour.$row['playerPosition']."</font></td>";
			}
			
			echo TD_NORMAL.GREY.$cPlayer[$i]->In."</font></td>";
			echo TD_NORMAL.GREY.$cPlayer[$i]->Out."</font></td>";
			echo TD_NORMAL.GREY.$cPlayer[$i]->Current."</font></td>";
			echo TD_NORMAL.RED.$playerScore."</font></td></tr>";
			echo "</form>";
		}
		echo "</tbody>
		</table></div>";
	}  //end else
}

function getPlayerScore($row, $maxWeeks)
{
	$playerScore = 0;
			
	for ($j = 1; $j <= $maxWeeks; $j++)
	{
		$week = getWeekVariable($j);
		$playerScore += $row[$week];
	}
	
	return $playerScore;
}



echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>
